﻿/*
 * ********************************************************************************************************************
 *
 *               iFHS7.
 *              ;BBMBMBMc                  rZMBMBR              BMB
 *              MBEr:;PBM,               7MBMMEOBB:             BBB                       RBW
 *     XK:      BO     SB.     :SZ       MBM.       c;;     ir  BBM :FFr       :SSF:    ;xBMB:r   iuGXv.    i:. iF2;
 *     DBBM0r.  :D     S7   ;XMBMB       GMBMu.     MBM:   BMB  MBMBBBMBMS   WMBMBMBBK  MBMBMBM  BMBRBMBW  .MBMBMBMBB
 *      :JMRMMD  ..    ,  1MMRM1;         ;MBMBBR:   MBM  ;MB:  BMB:   MBM. RMBr   sBMH   BM0         UMB,  BMB.  KMBv
 *     ;.   XOW  B1; :uM: 1RE,   i           .2BMBs  rMB. MBO   MBO    JMB; MBB     MBM   BBS    7MBMBOBM:  MBW   :BMc
 *     OBRJ.SEE  MRDOWOR, 3DE:7OBM       .     ;BMB   RMR7BM    BMB    MBB. BMB    ,BMR  .BBZ   MMB   rMB,  BMM   rMB7
 *     :FBRO0D0  RKXSXPR. JOKOOMPi       BMBSSWBMB;    BMBB:    MBMB0ZMBMS  .BMBOXRBMB    MBMDE RBM2;SMBM;  MBB   xBM2
 *         iZGE  O0SHSPO. uGZ7.          sBMBMBDL      :BMO     OZu:BMBK,     rRBMB0;     ,EBMB  xBMBr:ER.  RDU   :OO;
 *     ,BZ, 1D0  RPSFHXR. xWZ .SMr                  . .BBB
 *      :0BMRDG  RESSSKR. 2WOMBW;                   BMBMR
 *         i0BM: SWKHKGO  MBDv
 *           .UB  OOGDM. MK,                                          Copyright (c) 2015-2019.  斯伯坦机器人
 *              ,  XMW  ..
 *                  r                                                                     All rights reserved.
 *
 * ********************************************************************************************************************
 */

using MongoDB.Driver;
using System;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace MongoPI
{
    /// <summary>
    /// ConnectDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ConnectDialog : Window, INotifyPropertyChanged
    {
        /// <summary>
        /// 实现INotifyPropertyChanged接口
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #region 定义属性
        /// <summary>
        /// 服务名
        /// </summary>
        private string mServerName = "localhost";
        public string ServerName
        {
            get => mServerName;
            set
            {
                mServerName = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ServerName)));
            }
        }

        /// <summary>
        /// 服务器
        /// </summary>
        private string mServer = "localhost";
        public string Server
        {
            get => mServer;
            set
            {
                mServer = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Server)));
            }
        }

        /// <summary>
        /// 端口号
        /// </summary>
        private int mPort = 27017;
        public int Port
        {
            get => mPort;
            set
            {
                mPort = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Port)));
            }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        private string mUserName;
        public string UserName
        {
            get => mUserName;
            set
            {
                mUserName = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(UserName)));
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        private string mPassword;
        public string Password
        {
            get => mPassword;
            set
            {
                mPassword = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Password)));
            }
        }

        /// <summary>
        /// 数据库
        /// </summary>
        private string mDatabase = "local";
        public string Database
        {
            get => mDatabase;
            set
            {
                mDatabase = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Database)));
            }
        }

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string ConnectString
        {
            get
            {
                StringBuilder strBuilder = new StringBuilder(@"mongodb://",1024);
                if (null != UserName)
                {
                    strBuilder.Append($"{UserName}:{Password}@");
                }
                strBuilder.Append($"{Server}:{Port}");
                if (null != Database)
                {
                    strBuilder.Append($"/{Database}");
                }
                return strBuilder.ToString();
            }
        }
        #endregion

        /// <summary>
        /// 创建连接对话框构造函数
        /// </summary>
        public ConnectDialog()
        {
            InitializeComponent();
            DataContext = this;
            return;
        } // Function ConnectDialog()

        /// <summary>
        /// 点击“取消”按钮
        /// </summary>
        /// <param name="sender">事件发起者</param>
        /// <param name="e">事件参数</param>
        private void ButtonCancel_Click(object sender, ExecutedRoutedEventArgs e)
        {
            this.Close();
            return;
        } // Function ButtonCancel_Click()

        /// <summary>
        /// 点击“保存”按钮
        /// </summary>
        /// <param name="sender">事件发起者</param>
        /// <param name="e">事件参数</param>
        private void ButtonSave_Click(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show(ConnectString);
            return;
        } // Function ButtonSave_Click()

        /// <summary>
        /// 点击“测试”按钮
        /// </summary>
        /// <param name="sender">事件发起者</param>
        /// <param name="e">事件参数</param>
        private void ButtonTest_Click(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                MongoClient mongo = new MongoClient(ConnectString);
                try
                {
                    foreach(string db1 in mongo.ListDatabaseNames().ToList())
                    {
                        MessageBox.Show(db1);
                    }
                    var db = mongo.GetDatabase(Database);
                }
                catch (Exception e1)
                {
                    MessageBox.Show("abc" + e1.Message);
                }


            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }

            MessageBox.Show("MsgConnectSuccess".Resource());
            return;
        } // Function ButtonTest_Click()
    } // Class ConnectDialog
} // Namespace MongoPI
